package com.luocj.muke.demo.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.adapter.CountDownAdapter;
import com.luocj.muke.demo.model.CountDownModel;
import com.luocj.muke.demo.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class CountDownListActivity extends AppCompatActivity {

    private static final String TAG = CountDownListActivity.class.getSimpleName();
    private Context mContext;
    private RecyclerView recyclerview;
    private ArrayList<CountDownModel> countDownModels;
    private MyThread timeThread;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            int what = msg.what;
            switch (what) {
                case 1:
                    adapter.notifyData();
                    break;
            }

            super.handleMessage(msg);
        }
    };
    private CountDownAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_down_list);
        mContext = CountDownListActivity.this;
        initView();
    }

    private void initView() {
        recyclerview = findViewById(R.id.recyclerview_count_down);
        recyclerview.setLayoutManager(new LinearLayoutManager(CountDownListActivity.this));
        adapter = new CountDownAdapter(mContext, getDataList());
        recyclerview.setAdapter(adapter);

        //遍历所有数据，算出时间差并保存在每个商品的counttime属性内
        for (int i = 0; i < countDownModels.size(); i++) {
            long counttime = Utils.timeDifference(countDownModels.get(i).getNowTime(), countDownModels.get(i).getEndTime());
            countDownModels.get(i).setCountTime(counttime);
        }
        timeThread = new MyThread(countDownModels);
        new Thread(timeThread).start();
//        adapter.notifyDataSetChanged();

    }


    class MyThread implements Runnable {
        //用来停止线程
        boolean endThread;
        ArrayList<CountDownModel> models = new ArrayList<>();

        public MyThread(ArrayList<CountDownModel> models) {
            this.models = models;
        }

        @Override
        public void run() {
            while (!endThread) {
                try {
                    Thread.sleep(1000);
                    for (int i = 0; i < models.size(); i++) {
                        //拿到每件商品的时间差，转化为具体的多少天多少小时多少分多少秒
                        //并保存在商品time这个属性内
                        long counttime = models.get(i).getCountTime();
                        long days = counttime / (1000 * 60 * 60 * 24);
                        long hours = (counttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
                        long minutes = (counttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
                        long second = (counttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
                        //并保存在商品time这个属性内
                        String finaltime = days + "天" + hours + "时" + minutes + "分" + second + "秒";
                        models.get(i).setTime(finaltime);
                        //如果时间差大于1秒钟，将每件商品的时间差减去一秒钟，
                        // 并保存在每件商品的counttime属性内
                        if (counttime > 1000) {
                            models.get(i).setCountTime(counttime - 1000);
                        }
                    }
                    Message message = new Message();
                    message.what = 1;
                    //发送信息给handler
                    mHandler.sendMessage(message);
                } catch (Exception e) {

                }
            }
        }
    }


    private ArrayList<CountDownModel> getDataList() {
        countDownModels = new ArrayList<>();

        CountDownModel model = new CountDownModel();
        model.setName("item " + 0);
        model.setStartTime("2016-10-01 12:30:30");
        model.setEndTime("2017-01-15 17:50:23");
        model.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model);

        CountDownModel model1 = new CountDownModel();
        model1.setName("item " + 1);
        model1.setStartTime("2016-10-01 12:30:30");
        model1.setEndTime("2017-01-15 16:50:23");
        model1.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model1);

        CountDownModel model2 = new CountDownModel();
        model2.setName("item " + 2);
        model2.setStartTime("2016-10-01 12:30:30");
        model2.setEndTime("2017-01-15 15:50:23");
        model2.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model2);

        CountDownModel model3 = new CountDownModel();
        model3.setName("item " + 3);
        model3.setStartTime("2016-10-01 12:30:30");
        model3.setEndTime("2017-01-15 18:50:23");
        model3.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model3);

        CountDownModel model4 = new CountDownModel();
        model4.setName("item " + 4);
        model4.setStartTime("2016-10-01 12:30:30");
        model4.setEndTime("2017-01-15 18:50:23");
        model4.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model4);

        CountDownModel model5 = new CountDownModel();
        model5.setName("item " + 5);
        model5.setStartTime("2016-10-01 12:30:30");
        model5.setEndTime("2017-01-15 18:50:23");
        model5.setNowTime("2016-12-04 15:24:43");
        countDownModels.add(model5);

        return countDownModels;
    }
}
